"""
SkOpts is built differently than the rest of our source files. See //src/core/SkOpts.h for
a bit more background about what SkOpts is for and how it works.

Instead of bubbling all the headers and sources up to the top level where they are compiled in
one large library, we only bubble up the headers, so the default implementations in those files
can be built against the minimum version of Skia (which the whole library is compiled with).

We create several libraries that contain a single source file and may need to access any of
Skia's headers. These libraries are each compiled with a different set of compiler flags
(e.g. architecture options) and linked into the final library or binary.
"""

load("@skia_user_config//:copts.bzl", "DEFAULT_COPTS")
load("//bazel:flags.bzl", "selects")
load("//bazel:skia_rules.bzl", "exports_files_legacy", "skia_cc_deps", "skia_cc_library", "skia_filegroup")

package(
    default_applicable_licenses = ["//:license"],
    features = [
        # While the OPTS_HDRS are already pulled in as textual_headers,
        # any implicit dependency on the system headers also needs to be textual.
        "-use_header_modules",
    ],
)

licenses(["notice"])

exports_files_legacy()

skia_filegroup(
    name = "private_hdrs",
    srcs = [
        "SkBitmapProcState_opts.h",
        "SkBlitMask_opts.h",
        "SkBlitRow_opts.h",
        "SkMemset_opts.h",
        "SkOpts_RestoreTarget.h",
        "SkOpts_SetTarget.h",
        "SkRasterPipeline_opts.h",
        "SkSwizzler_opts.inc",
    ],
    visibility = [
        "//src:__pkg__",
    ],
)

skia_cc_library(
    name = "legacy_hsw",  # https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#Advanced_Vector_Extensions_2
    srcs = [
        "SkOpts_hsw.cpp",
        "//include/core:opts_srcs",
        "//include/private:opts_srcs",
        "//include/private/base:private_hdrs",
        "//src/base:private_hdrs",
        "//src/core:opts_srcs",
        "//src/shaders:opts_srcs",
        "//src/sksl/tracing:opts_srcs",
    ],
    copts = DEFAULT_COPTS + ["-march=haswell"],
    textual_hdrs = [
        "SkRasterPipeline_opts.h",
    ],
    deps = [
        "//modules/skcms",  # Needed to implement SkRasterPipeline_opts.h
        "@skia_user_config//:user_config",
    ],
)

skia_cc_library(
    name = "legacy_skx",
    srcs = [
        "SkOpts_skx.cpp",
        "//include/core:opts_srcs",
        "//include/private:opts_srcs",
        "//include/private/base:private_hdrs",
        "//src/base:private_hdrs",
        "//src/core:opts_srcs",
        "//src/shaders:opts_srcs",
        "//src/sksl/tracing:opts_srcs",
    ],
    copts = DEFAULT_COPTS + ["-march=skylake-avx512"],
    textual_hdrs = [
        "SkRasterPipeline_opts.h",
    ],
    deps = [
        "//modules/skcms",  # Needed to implement SkRasterPipeline_opts.h
        "@skia_user_config//:user_config",
    ],
)

skia_cc_deps(
    name = "deps",
    visibility = [
        "//:__pkg__",  # Needed in G3
        "//src:__pkg__",
    ],
    deps = selects.with_or({
        ("@platforms//cpu:x86_64", "@platforms//cpu:x86_32"): [
            ":legacy_hsw",
            ":legacy_skx",
        ],
        # We have no architecture specific optimizations for ARM64 right now
        "@platforms//cpu:arm64": [],
        # None of these opts work on WASM, so do not even bother compiling them.
        "//bazel/common_config_settings:cpu_wasm": [],
        "//conditions:default": [],
    }),
)

skia_cc_library(
    name = "hsw",  # https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#Advanced_Vector_Extensions_2
    srcs = [
        "SkOpts_hsw.cpp",
        "//include/core:opts_srcs",
        "//include/private:opts_srcs",
        "//src/core:opts_srcs",
        "//src/shaders:opts_srcs",
        "//src/sksl/tracing:opts_srcs",
    ],
    copts = DEFAULT_COPTS + ["-march=haswell"],
    textual_hdrs = [
        "SkRasterPipeline_opts.h",
        ":private_hdrs",
    ],
    deps = [
        "//modules/skcms",  # Needed to implement SkRasterPipeline_opts.h
        "//src/base",
        "@skia_user_config//:user_config",
    ],
)

skia_cc_library(
    name = "skx",
    srcs = [
        "SkOpts_skx.cpp",
        "//include/core:opts_srcs",
        "//include/private:opts_srcs",
        "//src/core:opts_srcs",
        "//src/shaders:opts_srcs",
        "//src/sksl/tracing:opts_srcs",
    ],
    copts = DEFAULT_COPTS + ["-march=skylake-avx512"],
    textual_hdrs = [
        "SkRasterPipeline_opts.h",
        ":private_hdrs",
    ],
    deps = [
        "//modules/skcms",  # Needed to implement SkRasterPipeline_opts.h
        "//src/base",
        "@skia_user_config//:user_config",
    ],
)

skia_cc_deps(
    name = "opts",
    visibility = ["//src/core:__pkg__"],
    deps = selects.with_or({
        ("@platforms//cpu:x86_64", "@platforms//cpu:x86_32"): [
            ":hsw",
            ":skx",
        ],
        "//bazel/common_config_settings:cpu_wasm": [],
        "//conditions:default": [],
    }),
)

skia_filegroup(
    name = "textual_hdrs",
    srcs = [
        "SkBitmapProcState_opts.h",
        "SkBlitMask_opts.h",
        "SkBlitRow_opts.h",
        "SkMemset_opts.h",
        "SkOpts_RestoreTarget.h",
        "SkOpts_SetTarget.h",
        "SkRasterPipeline_opts.h",
        "SkSwizzler_opts.inc",
    ],
    visibility = [
        "//src/core:__pkg__",
    ],
)
